package com.sunnada.edu.system.pub.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.filter.XssHttpServletRequestWrapper;
import com.sunnada.edu.system.pub.service.AppAttendanceService;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.quartz.timers.AttendanceMonit;


/***
 * explain 考勤记录
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/appAttendanceController")
public class AppAttendanceController extends BasicController {
	Logger log  =  Logger.getLogger(AppAttendanceController.class );
	@Resource
	AppAttendanceService appAttendanceService;

	/** 考勤记录保存功能 
	 * @throws UnsupportedEncodingException */
	@RequestMapping("/appAttendanceSave")
	public void appAttendanceSave(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		HashMap<String, String> params = new HashMap<String, String>();
		String jobno = re.getParameter("jobno");
		String signTime = re.getParameter("signTime");
		String machineCode = re.getParameter("machineCode");
		String punchType = re.getParameter("punchType");
		String addressCode = re.getParameter("addressCode");
		String addressName = re.getParameter("addressName");
		log.info("------------[接收数据jobno="+jobno+" ]----------------");
		log.info("------------[接收数据signTime="+signTime+" ]----------------");
		log.info("------------[接收数据machineCode="+machineCode+" ]----------------");
		log.info("------------[接收数据punchType="+punchType+" ]----------------");
		log.info("------------[接收数据addressCode="+addressCode+" ]----------------");
		log.info("------------[接收数据addressName="+addressName+" ]----------------");
		if(Utils.isNotNull(jobno) && Utils.isNotNull(machineCode) && Utils.isNotNull(punchType)&& Utils.isNotNull(addressName)){
			addressName=URLDecoder.decode(addressName, HTTP.UTF_8);
			String recId = "";
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			params.put("jobno", jobno);
			params.put("signTime",signTime );
			params.put("machineCode",machineCode);
			params.put("punchType", punchType);
			params.put("addressCode",addressCode);
			params.put("addressName",addressName);
			msg = appAttendanceService.appAttendanceAdd(params);
		}else{
			msg = "0";
		}
		JSONObject returnJson = new JSONObject();
		if(msg.equals("1")){
			returnJson.put("message", "保存成功");
			returnJson.put("status", "1");
		}else{
			returnJson.put("message", "保存失败");
			returnJson.put("status", "0");
		}
		printInfo(response, returnJson);
	}
	
	
	@RequestMapping("/httpTest")
	public void httpTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
		log.info("------------[ 请求接收 ]----------------");
	/*	HashMap<String, String> params = new HashMap<String, String>();
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		String attendance = re.getParameter("appAttendance");
		log.info("------------[ 请求内容:"+URLDecoder.decode(attendance, "utf-8")+"]----------------");
		JSONObject obj =JSONObject.parseObject(URLDecoder.decode(attendance, "utf-8"));
		if(obj!= null && Utils.isNotNull(obj)){
			String jobno = obj.getString("jobno");
			String signTime = obj.getString("signTime");
			String machineCode = obj.getString("machineCode");
			String punchType = obj.getString("punchType");
			String addressCode = obj.getString("addressCode");
			String addressName = obj.getString("addressName");
			if(Utils.isNotNull(jobno) && Utils.isNotNull(machineCode) && Utils.isNotNull(punchType)&& Utils.isNotNull(addressName)){
				String recId = "";
				recId = UUID.randomUUID().toString();
				params.put("recId", recId);
				params.put("jobno", jobno);
				params.put("signTime",signTime );
				params.put("machineCode",machineCode);
				params.put("punchType", punchType);
				params.put("addressCode",addressCode);
				params.put("addressName",addressName);
				msg = appAttendanceService.appAttendanceAdd(params);
			}else{
				msg = "0";
			}
		}else{
			msg = "0";
		}
		JSONObject json = new JSONObject();
		if(msg.equals("1")){
			json.put("message", "保存成功");
			json.put("status", "1");
		}else{
			json.put("message", "保存失败");
			json.put("status", "0");
		}
		printInfo(response, json);*/
		
		
		
		
		
		HashMap<String, String> params = new HashMap<String, String>();
		String jobno = request.getParameter("jobno");
		String signTime = request.getParameter("signTime");
		String machineCode = request.getParameter("machineCode");
		String punchType = request.getParameter("punchType");
		String addressCode = request.getParameter("addressCode");
		String addressName = request.getParameter("addressName");
		if(Utils.isNotNull(jobno) && Utils.isNotNull(machineCode) && Utils.isNotNull(punchType)&& Utils.isNotNull(addressName)){
			/*jobno=URLDecoder.decode(jobno, HTTP.UTF_8);
			signTime=URLDecoder.decode(signTime, HTTP.UTF_8);
			machineCode=URLDecoder.decode(machineCode, HTTP.UTF_8);
			punchType=URLDecoder.decode(punchType, HTTP.UTF_8);
			addressCode=URLDecoder.decode(addressCode, HTTP.UTF_8);*/
			addressName=URLDecoder.decode(addressName, HTTP.UTF_8);
			String recId = "";
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			params.put("jobno", jobno);
			params.put("signTime",signTime );
			params.put("machineCode",machineCode);
			params.put("punchType", punchType);
			params.put("addressCode",addressCode);
			params.put("addressName",addressName);
			msg = appAttendanceService.appAttendanceAdd(params);
		}else{
			msg = "0";
		}
		JSONObject returnJson = new JSONObject();
		if(msg.equals("1")){
			returnJson.put("message", "保存成功");
			returnJson.put("status", "1");
		}else{
			returnJson.put("message", "保存失败");
			returnJson.put("status", "0");
		}
		printInfo(response, returnJson);
		
		
		
		
		
		 // 读取请求内容
       /* BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        // 将资料解码
        String reqBody = sb.toString();
        String json=URLDecoder.decode(reqBody, HTTP.UTF_8);
        log.info("------------[ 请求内容:"+json+"]----------------");
        JSONObject obj=JSONObject.parseObject(json);
        HashMap<String, String> params = new HashMap<String, String>();
    	if(obj!= null && Utils.isNotNull(obj)){
			String recId = "";
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			params.put("jobno", obj.getString("jobno"));
			params.put("signTime",Utils.trimString(obj.getString("signTime")) );
			params.put("machineCode",Utils.trimString(obj.getString("machineCode")));
			params.put("punchType", Utils.trimString(obj.getString("punchType")));
			params.put("addressCode",Utils.trimString(obj.getString("addressCode")) );
			params.put("addressName",Utils.trimString(obj.getString("addressName")) );
			msg = appAttendanceService.appAttendanceAdd(params);
			//msg = "1";
		}else{
			msg = "0";
		}
		JSONObject returnJson = new JSONObject();
		if(msg.equals("1")){
			returnJson.put("message", "保存成功");
			returnJson.put("status", "1");
		}else{
			returnJson.put("message", "保存失败");
			returnJson.put("status", "0");
		}
		JSONObject returnJson = new JSONObject();
		returnJson.put("message", "保存成功");
		returnJson.put("status", "1");
		printInfo(response, returnJson);*/
	}	

}
